package com.ruoyi.coal.modules.coalpile.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.coal.common.utils.PageUtils;
import com.ruoyi.coal.common.utils.R;
import com.ruoyi.coal.modules.coalpile.domain.CoalYardInfoEntity;
import com.ruoyi.coal.modules.coalpile.domain.CutUpEntity;
import com.ruoyi.coal.modules.coalpile.domain.PointEntity;
import com.ruoyi.coal.modules.coalpile.domain.vo.CutUPCircleVo;
import com.ruoyi.coal.modules.coalpile.domain.vo.CutUpVo;
import com.ruoyi.coal.modules.coalpile.mapper.CutUpDao;
import com.ruoyi.coal.modules.coalpile.service.CoalYardInfoService;
import com.ruoyi.coal.modules.coalpile.service.CutUpService;
import com.ruoyi.coal.modules.coalpile.service.PointService;
import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.enums.DataSourceType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

@Api(value = "cutup", tags = {"煤场分隔信息"})
@RestController
@RequestMapping({"coalpile/cutup"})
@DataSource(value = DataSourceType.SLAVE)
public class CutUpController {
    @Autowired
    private CutUpService cutUpService;

    @Resource
    private CutUpDao cutUpDao;

    @Resource
    private PointService pointService;

    @Resource
    private CoalYardInfoService coalYardInfoService;

    @ApiOperation(value = "分页查询", notes = "分页查询")
    @ApiImplicitParams({@ApiImplicitParam(name = "page", value = "当前页码,从1开始", paramType = "query", required = true, dataType = "int"), @ApiImplicitParam(name = "limit", value = "每页显示记录数", paramType = "query", required = true, dataType = "int"), @ApiImplicitParam(name = "sidx", value = "排序字段", paramType = "query", dataType = "String"), @ApiImplicitParam(name = "order", value = "排序方式，可选值(asc,desc)", paramType = "query", dataType = "String"), @ApiImplicitParam(name = "key", value = "关键字", paramType = "query", dataType = "String")})
    @GetMapping({"/list"})
    @RequiresPermissions({"coalpile:cutup:list"})
    public R list(@ApiIgnore @RequestParam Map<String, Object> params) {
        PageUtils page = this.cutUpService.queryPage(params);
        return R.ok().put("page", page);
    }

    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping({"/info/{id}"})
    @RequiresPermissions({"coalpile:cutup:info"})
    public R info(@PathVariable("id") Integer id) {
        CutUpEntity cutUp = (CutUpEntity) this.cutUpService.getById(id);
        return R.ok().put("cutUp", cutUp);
    }

    @ApiOperation(value = "添加方形分隔(用于前端调用)", notes = "添加方形分隔(用于前端调用)")
    @PostMapping({"/saveCutUp"})
    @RequiresPermissions({"coalpile:cutup:save"})
    public R saveCutUp(@RequestBody CutUpVo cutUp) {
        this.cutUpService.saveCutUp(cutUp);
        return R.ok();
    }

    @ApiOperation(value = "添加圆形分隔(用于前端调用)", notes = "添加圆分隔(用于前端调用)")
    @PostMapping({"/saveCircleCutUp"})
    @RequiresPermissions({"coalpile:cutup:save"})
    public R saveCircleCutUp(@RequestBody CutUPCircleVo cutUp) {
        this.cutUpService.saveCircleCutUp(cutUp);
        return R.ok();
    }

    @ApiOperation(value = "新增煤场分隔信息", notes = "新增煤场分隔信息")
    @PostMapping({"/save"})
    @RequiresPermissions({"coalpile:cutup:save"})
    public R save(@RequestBody CutUpEntity cutUp) {
        this.cutUpService.save(cutUp);
        return R.ok();
    }

    @ApiOperation(value = "根据id修改煤场分隔信息", notes = "根据id修改煤场分隔信息")
    @PostMapping({"/update"})
    @RequiresPermissions({"coalpile:cutup:update"})
    public R update(@RequestBody CutUpEntity cutUp) {
        this.cutUpService.updateById(cutUp);
        return R.ok();
    }

    @ApiOperation(value = "通过id删除煤场分隔信息", notes = "通过id删除煤场分隔信息")
    @PostMapping({"/delete"})
    @RequiresPermissions({"coalpile:cutup:delete"})
    public R delete(@RequestBody Integer[] ids) {
        this.cutUpService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }

    @ApiOperation(value = "通过煤场ID查询分隔信息", notes = "通过煤场ID查询分隔信息")
    @GetMapping({"/getCutUpByCoalYardId"})
    @RequiresPermissions({"coalpile:cutup:info"})
    public R getCutUpByCoalYardId(@RequestParam("coalYardId") Integer coalYardId) {
        if (coalYardId == null)
            return R.error("煤场ID不能为空");
        QueryWrapper<CutUpEntity> cutUpEntityQueryWrapper = new QueryWrapper();
        List<CutUpEntity> cutUpEntities = this.cutUpDao.selectList((Wrapper) cutUpEntityQueryWrapper.eq("coal_yard_id", coalYardId));
        if (cutUpEntities.size() == 0)
            return R.error("该煤场没有分隔信息");
        ArrayList<CutUpVo> cutUpVos = new ArrayList<>();
        for (CutUpEntity cutUpEntity : cutUpEntities) {
            CutUpVo cutUpVo = new CutUpVo();
            cutUpVo.setName(cutUpEntity.getName());
            cutUpVo.setCutUpId(cutUpEntity.getId());
            cutUpVo.setCoalYardId(cutUpEntity.getCoalYardId());
            cutUpVo.setXa1((PointEntity) this.pointService.getById(cutUpEntity.getXa1()));
            cutUpVo.setXa2((PointEntity) this.pointService.getById(cutUpEntity.getXa2()));
            cutUpVo.setXb1((PointEntity) this.pointService.getById(cutUpEntity.getXb1()));
            cutUpVo.setXb2((PointEntity) this.pointService.getById(cutUpEntity.getXb2()));
            cutUpVo.setYa1((PointEntity) this.pointService.getById(cutUpEntity.getYa1()));
            cutUpVo.setYa2((PointEntity) this.pointService.getById(cutUpEntity.getYa2()));
            cutUpVo.setYb1((PointEntity) this.pointService.getById(cutUpEntity.getYb1()));
            cutUpVo.setYb2((PointEntity) this.pointService.getById(cutUpEntity.getYb2()));
            cutUpVo.setCoalPileDensity(cutUpEntity.getCoalPileDensity());
            cutUpVo.setCoalPileGround(cutUpEntity.getCoalPileGround());
            cutUpVos.add(cutUpVo);
        }
        return R.ok().put("data", cutUpVos);
    }

    @ApiOperation(value = "根据煤场id删除分隔信息", notes = "根据煤场id删除分隔信息")
    @GetMapping({"/deleteCutUpByCoalYardId"})
    public R deleteCutUpByCoalYardId(@RequestParam("coalYardId") Integer coalYardId) {
        String coalYardName = ((CoalYardInfoEntity) this.coalYardInfoService.getById(coalYardId)).getName();
        if (coalYardId == null)
            return R.error("煤场ID不能为空");
        QueryWrapper<CutUpEntity> cutUpEntityQueryWrapper = new QueryWrapper();
        List<CutUpEntity> cutUpEntities = this.cutUpDao.selectList((Wrapper) ((QueryWrapper) cutUpEntityQueryWrapper.eq("coal_yard_id", coalYardId)).ne("name", coalYardName));
        if (cutUpEntities.size() == 0)
            return R.error("该煤场没有分隔信息");
        for (CutUpEntity cutUpEntity : cutUpEntities)
            this.cutUpDao.deleteById(cutUpEntity.getId());
        return R.ok();
    }
}
